define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            //初始化数据

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'crm/cooperation/index/',
                    add_url: 'crm/cooperation/add',
                    edit_url: 'crm/cooperation/edit',
                    del_url: 'crm/cooperation/del',
                    multi_url: 'crm/cooperation/multi',
                    table: 'cooperation',
                },
                queryParams: function (params) {
                    // 自定义搜索条件
                    var filter = params.filter ? JSON.parse(params.filter) : {};
                    var op = params.op ? JSON.parse(params.op) : {};
                    //filter.后跟的是在ajax里使用的名称\
                    params['wf_group_id'] = $('#group').val();
                    params['wf_admin_id'] = $('#admin_id').val();
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'buser.username', title: __('Buser.username'),operate:'LIKE'},
                        {field: 'product.product', title: __('Product.product'),operate:'LIKE'},
                        {field: 'myname', title: __('Myname'),operate:'LIKE'},
                        {field: 'mytel', title: __('Mytel'),operate:'LIKE'},
                        {field: 'othername', title: __('Othername'),operate:'LIKE'},
                        {field: 'othertel', title: __('Othertel'),operate:'LIKE'},
                        {field: 'mode', title: __('Mode'),operate:'LIKE'},
                        {field: 'much', title: __('Much'), operate:'BETWEEN'},
                        {field: 'assessment', title: __('Assessment'),operate:false},
                        {field: 'evaluate', title: __('Evaluate'),operate:false},
                        /*{field: 'chartimages', title: __('Chartimages'), formatter: Table.api.formatter.images,operate:false},*/
                        {field: 'cooperationfiles', title: __('Cooperationfiles'),formatter:Table.api.formatter.url,operate:false},
                        {field: 'risetime', title: __('Risetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'endtime', title: __('Endtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'admin.nickname', title: __('Admin.nickname')},
                        {field: 'admin_name', title: __('Admin_name')},
                        {field: 'statuslist', title: __('Statuslist'), searchList: {"1":__('Statuslist 1'),"2":__('Statuslist 2')}, formatter: Table.api.formatter.normal},
                        {field: 'typelist', title: __('Typelist'), searchList: {"1":__('Typelist 1'),"2":__('Typelist 2')}, formatter: Table.api.formatter.normal},
                        {field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-edit", null);
                                $(table).data("operate-del", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            },
                            buttons:[
                                {
                                    name: '评价',
                                    extend: 'data-area=\'["350px", "200px"]\'',
                                    text: __('评价'),
                                    title: __('评价'),
                                    classname: 'btn btn-xs btn-primary btn-dialog btn-success',
                                    icon: 'fa fa-check',
                                    url: 'crm/Cooperation/evaluate',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    },
                                    hidden:function (value) {
                                        if(admin_group_id!=2){
                                            return true
                                        }
                                    }
                                }
                            ]
                        }
                    ]
                ]
            });
            //当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                console.log(data);
                //这里我们手动设置底部的值
               $('#shouMoney').text(data['shouMoney']);
               $('#xuMoney').text(data['xuMoney'])
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        evaluate:function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});